//
//  WWCategoryCollectionViewCell.m
//  YiQiTingShu
//
//  Created by WeiWei on 15/11/3.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "WWCategoryCollectionViewCell.h"
#import "WWCategoryCollectionViewCellOfImageCell.h"
#import "WWCategoryCollectionViewCellOfLabelCell.h"
#import "WWCollectionViewHeaderCell.h"
#import "PublicHeader.h"
#import "WWModel.h"
#import <MBProgressHUD.h>

@interface WWCategoryCollectionViewCell () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) MBProgressHUD *progressView;

@end

@implementation WWCategoryCollectionViewCell

- (void)getData {
    self.dataSource = [NSMutableArray array];
    [AFTools getDataByURLString:@"http://mobile.ximalaya.com/mobile/discovery/v1/categories?device=iPhone&picVersion=11&scale=2" WithDataBlock:^(id data) {
        
        for (NSDictionary *dic in [data objectForKey:@"list"]) {
            WWModel *model = [WWModel wwModelWithDictionary:dic];
            [self.dataSource addObject:model];
        }
        
        _imageArray = [NSMutableArray array];
        _labelArray = [NSMutableArray array];
        for (int i = 0; i < 5; i++) {
            [_imageArray addObject:_dataSource[i]];
        }
        for (int i = 5; i < _dataSource.count; i++) {
            [_labelArray addObject:_dataSource[i]];
        }
        
        [self.wwCollectionView reloadData];
        [_progressView removeFromSuperview];
    }];
  
}


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UICollectionViewFlowLayout *wwFlow = [[UICollectionViewFlowLayout alloc] init];
        wwFlow.minimumInteritemSpacing = 0.0;
        wwFlow.minimumLineSpacing = 2;
        wwFlow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        self.wwCollectionView = [[UICollectionView alloc] initWithFrame:self.contentView.frame collectionViewLayout:wwFlow];
        self.wwCollectionView.delegate = self;
        self.wwCollectionView.dataSource = self;
        self.wwCollectionView.backgroundColor = [UIColor whiteColor];
        [self.wwCollectionView registerClass:[WWCategoryCollectionViewCellOfImageCell class] forCellWithReuseIdentifier:@"imageCell"];
         [self.wwCollectionView registerClass:[WWCollectionViewHeaderCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerCell"];
        [self.wwCollectionView registerClass:[WWCategoryCollectionViewCellOfLabelCell class] forCellWithReuseIdentifier:@"labelCell"];
        [self.contentView addSubview:self.wwCollectionView];
        _progressView = [[MBProgressHUD alloc] initWithView:self.wwCollectionView];
        _progressView.labelText = @"努力加载中,请稍等";
        [_progressView setMode:MBProgressHUDModeIndeterminate];
        [_progressView show:YES];
        _progressView.backgroundColor = [UIColor colorWithRed:0.58 green:0.6 blue:0.61 alpha:0.3];
        [self.wwCollectionView addSubview:self.progressView];
        [self getData];
        [self.wwCollectionView NightWithType:UIViewColorTypeNormal];
    }
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    self.wwCollectionView.frame = self.contentView.frame;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 5;
    }
    return 6;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (_labelArray.count == 0) {
        return 0;
    }
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row < 5 && indexPath.row >= 0) {
        WWCategoryCollectionViewCellOfImageCell *wwImageCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imageCell" forIndexPath:indexPath];
        wwImageCell.backgroundColor = [UIColor grayColor];
        
        if (indexPath.row <= self.imageArray.count) {
            WWModel *model = self.imageArray[indexPath.row];
            wwImageCell.model = model;
        }
        return wwImageCell;
    } else if(indexPath.section == 1){
        WWCategoryCollectionViewCellOfLabelCell *wwLabelCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"labelCell" forIndexPath:indexPath];
        if (indexPath.row <= self.labelArray.count) {
            WWModel *model = self.labelArray[indexPath.row];
            wwLabelCell.model = model;
        }
        return wwLabelCell;
    }else if(indexPath.section == 2){
        WWCategoryCollectionViewCellOfLabelCell *wwLabelCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"labelCell" forIndexPath:indexPath];
        if (indexPath.row <= self.labelArray.count) {
            WWModel *model = self.labelArray[indexPath.row + 6];
            wwLabelCell.model = model;
        }
        return wwLabelCell;
    }else{
        WWCategoryCollectionViewCellOfLabelCell *wwLabelCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"labelCell" forIndexPath:indexPath];
        if (indexPath.row <= self.labelArray.count) {
            WWModel *model = self.labelArray[indexPath.row + 12];
            wwLabelCell.model = model;
        }
        return wwLabelCell;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(self.contentView.frame.size.width / 5 - FitConstant_XorWidth(4), FitConstant_YorHeight(75));
    }
    if(indexPath.section < 4 && indexPath.section > 0) {

        return CGSizeMake(self.contentView.frame.size.width / 2, FitConstant_YorHeight(60));
    } else {
        return CGSizeMake(0, 0);
    }
        
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        WWCollectionViewHeaderCell *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerCell" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            [header.wwHeaderLabel NightWithType:UIViewColorType3];
        } else {
            [header.wwHeaderLabel NightWithType:UIViewColorType3];
        }
        return header;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.contentView.frame.size.width, FitConstant_YorHeight(10));
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        WWModel *model = self.dataSource[indexPath.row];
        [self.wwDelegate wwPushVCWithId:model.caretortId withTitle:model.title];
    }
    if (indexPath.section == 1) {
        WWModel *model = self.dataSource[indexPath.row + 5];
        [self.wwDelegate wwPushVCWithId:model.caretortId withTitle:model.title];
    }
    if (indexPath.section == 2) {
        WWModel *model = self.dataSource[indexPath.row + 11];
        [self.wwDelegate wwPushVCWithId:model.caretortId withTitle:model.title];
    }
    if (indexPath.section == 3) {
        WWModel *model = self.dataSource[indexPath.row + 17];
        [self.wwDelegate wwPushVCWithId:model.caretortId withTitle:model.title];
    }
}

@end
